package com.habbot.beautifyinghandbook;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;

import com.habbot.beautifyinghandbook.adapters.FavouriteAdapter;
import com.habbot.beautifyinghandbook.helpers.AppConfig;
import com.habbot.beautifyinghandbook.models.ItemTable;
import com.habbot.beautifyinghandbook.objects.ItemObject;

import de.timroes.swipetodismiss.SwipeDismissList;
import de.timroes.swipetodismiss.SwipeDismissList.UndoMode;
import de.timroes.swipetodismiss.SwipeDismissList.Undoable;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.ProgressBar;

public class FavouriteActivity extends BaseActivity {

	ProgressBar mProgressBar;
	ListView mListView;
	FavouriteAdapter mAdapter;
	SwipeDismissList.OnDismissCallback mCallback;
	SharedPreferences mSharedPreferences;

	@Override
	protected void onCreate(Bundle instance) {
		super.onCreate(instance);
		setContentView(R.layout.activity_favourite);
		initComponents();
		initAdView(R.id.ad);
		addListeners();
		new LoadFavouriteItemTask().execute();
	}

	@Override
	void initComponents() {
		// Set Navigation
		addNavigation(getString(R.string.favorite),
				R.drawable.btn_back_selector, 0);
		setNavigationBackground(R.drawable.ic_topbar3);

		mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
		mProgressBar.setVisibility(View.INVISIBLE);

		// Init Listview
		mListView = (ListView) findViewById(R.id.list);
		mCallback = new SwipeDismissList.OnDismissCallback() {
			@Override
			public Undoable onDismiss(AbsListView listView, final int position) {
				final ItemObject itemToDelete = mAdapter.getItem(position);
				mAdapter.remove(itemToDelete);
				Set<String> items = mSharedPreferences.getStringSet(
						AppConfig.EXTRA_FAVOURITE,
						new CopyOnWriteArraySet<String>());
				items.remove(itemToDelete.getId() + "");
				Editor editor = mSharedPreferences.edit();
				editor.putStringSet(AppConfig.EXTRA_FAVOURITE, items);
				editor.commit();
				return new SwipeDismissList.Undoable() {
					public void undo() {
						mAdapter.insert(itemToDelete, position);
						Set<String> items = mSharedPreferences.getStringSet(
								AppConfig.EXTRA_FAVOURITE,
								new CopyOnWriteArraySet<String>());
						items.add(itemToDelete.getId() + "");
						Editor editor = mSharedPreferences.edit();
						editor.putStringSet(AppConfig.EXTRA_FAVOURITE, items);
						editor.commit();
					}
				};
			}
		};
		UndoMode mode = SwipeDismissList.UndoMode.SINGLE_UNDO;
		SwipeDismissList swipeList = new SwipeDismissList(mListView, mCallback,
				mode);
		swipeList.setAutoHideDelay(3000);
		mListView.setOnTouchListener(swipeList);

		mSharedPreferences = getSharedPreferences(AppConfig.SHARE_PREFERENCE,
				MODE_PRIVATE);
	}

	@Override
	void addListeners() {
		setNavIcLeftClickListener(this);
		mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapterView, View view, int position,
					long id) {
				Intent intent=new Intent(FavouriteActivity.this, ItemContentActivity.class);
				intent.putExtra("ITEM", mAdapter.getItem(position));
				startActivityForResult(intent, 100);
				overridePendingTransition(R.anim.slide_in_right,
						R.anim.slide_out_left);
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.nav_ic_left:
			finish();
			break;

		default:
			break;
		}
	}

	@Override
	public void finish() {
		super.finish();
		overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
	}

	private class LoadFavouriteItemTask extends
			AsyncTask<Void, ItemObject, List<ItemObject>> {
		private ItemTable itemTable;

		@Override
		protected List<ItemObject> doInBackground(Void... params) {
			itemTable = new ItemTable(FavouriteActivity.this);
			List<ItemObject> items = new ArrayList<ItemObject>();
			Set<String> ids = mSharedPreferences.getStringSet(
					AppConfig.EXTRA_FAVOURITE, null);
			if (ids != null && ids.size() == 0) {
				for (String id : ids) {
					ItemObject item = itemTable.getItemById(id);
					if (item != null)
						items.add(item);
				}
			}
			Collections.sort(items, new Comparator<ItemObject>() {

				@Override
				public int compare(ItemObject lhs, ItemObject rhs) {
					return lhs.getTitle().compareTo(rhs.getTitle());
				}
			});
			return items;
		}

		@Override
		protected void onPostExecute(List<ItemObject> result) {
			mListView.setAdapter(mAdapter = new FavouriteAdapter(
					FavouriteActivity.this, result));
			mListView.setVisibility(View.VISIBLE);
			mProgressBar.setVisibility(View.INVISIBLE);
			super.onPostExecute(result);
		}

		@Override
		protected void onPreExecute() {
			mListView.setVisibility(View.INVISIBLE);
			mProgressBar.setVisibility(View.VISIBLE);
			super.onPreExecute();
		}

	}
}
